home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cp1.zip / FILELEN.C! < prev    next >
Text File  |  1993-03-30  |  3KB  |  85 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville MI
  3. Date: 03-28-93 (17:22)             Number: 37
  4. From: BOB STOUT                    Refer#: 34
  5.   To: FRED COLE                     Recvd: NO  
  6. Subj: FINDING FILE LENGTH            Conf: (36) C Language
  7. ---------------------------------------------------------------------------
  8. In a message of <Mar 26 20:09>, Fred Cole (1:231/50.4@fidonet.org) writes:
  9.  
  10.  >I have a nasty habit of concatenating to the end of files and not paying
  11.  >attention to any ^Z chars that may be lurking about.  Some editors are VERY
  12.  >unforgiving and I don't always remember to check before I save a file. Thank
  13.  >God for Peter Norton!  May he live long and prosper. <g>
  14.  
  15.   No need, use this from the next SNIPPETS:
  16.  
  17. /*
  18. **  STRIPEOF.C
  19. **
  20. **  public domain demo by Bob Stout
  21. */
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <io.h>
  26. #include <fcntl.h>
  27.  
  28. #define BUFSIZE 16384
  29.  
  30. int main(int argc, char *argv[])
  31. {
  32.       char *buf;
  33.  
  34.       if (2 > argc)
  35.       {
  36.             puts("Usage: STRIPEOF filename1 [...filenameN]");
  37.             return EXIT_FAILURE;
  38.       }
  39.       if (NULL == (buf = malloc(BUFSIZE)))
  40.       {
  41.             puts("STRIPEOF internal failure");
  42.             return EXIT_FAILURE;
  43.       }
  44.       while (--argc)
  45.       {
  46.             int fd;
  47.             size_t bytes;
  48.             int found = 0;
  49.             long zpos = 0L;
  50.  
  51.             if (-1 == (fd = open(*(++argv), O_RDWR | O_BINARY)))
  52.             {
  53.                   printf("Couldn't open %s\n", *argv);
  54.                   return EXIT_FAILURE;
  55.             }
  56.             while (0 < (bytes = read(fd, buf, BUFSIZE)))
  57.             {
  58.                   int i;
  59.  
  60.                   for (i = 0; i < (int)bytes; ++i)
  61.                   {
  62.                         if (('Z' - 64) == buf[i])
  63.                         {
  64.                               found = 1;
  65.                               zpos += i;
  66.                               break;
  67.                         }
  68.                   }
  69.                   if (found)
  70.                         break;
  71.                   zpos += bytes;
  72.             }
  73.             if (found)
  74.                   chsize(fd, zpos);
  75.       }
  76.       return EXIT_SUCCESS;
  77. }
  78.  
  79.  
  80. --- QM v1.00
  81.  * Origin: MicroFirm : Down to the C in chips (1:106/2000.6)
  82. SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
  83. SEEN-BY: 153/752 154/40 77 157/2 159/100 125 575 950 203/23 209/209 280/1
  84. SEEN-BY: 390/1 396/1 5 15 730/6 2270/1 3603/20
  85.